/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package carreras.datos.connection;
import java.io.FileNotFoundException;

/**
 * Una especie de pool de conexiones
 * @author fanky10
 */
public class DataQueueManager {
    private static DataQueueManager single_dqm;
    private DBConnector single_dbconnector;
    private int internal_status;

    private DataQueueManager(){
        this.internal_status = DESOCUPADO;
    }
    //<editor-fold defaultstate="collapsed" desc="singleton">
    public synchronized static DataQueueManager getInstance(){
        if(single_dqm==null)
            createInstance();
        return single_dqm;
    }
    private static void createInstance(){
        single_dqm = new DataQueueManager();
    }
    //</editor-fold>


    //<editor-fold defaultstate="collapsed" desc="db-singleton">
    public synchronized DBConnector getDBConn(){
        if(single_dbconnector==null){
            createDBConnectorInstance();
        }
        return single_dbconnector;
    }
    private synchronized void createDBConnectorInstance(){
        try{
            single_dbconnector = new SQLite();
        }catch(ClassNotFoundException ex){
            warn(ex.getMessage());
        }catch(InstantiationException ex){
            warn(ex.getMessage());
        }catch(IllegalAccessException ex){
            warn(ex.getMessage());
        }
    }
    //</editor-fold>    
    

    //<editor-fold defaultstate="collapsed" desc="server-design">
   private static final int OCUPADO = 1;
   private static final int DESOCUPADO = 0;
//   private static final int SEMIOCUPADO = 2;
   private int queue_length=0;

    public synchronized DBConnector obtenerConector(){
        debug("new getter v3 (while--ifagain)");
        while(getInternal_status() == OCUPADO){//is Server busy?
            //increment n.in.queue
            incrementQueue();
            debug("servidor ocupado... estoy en el nro_cola: "+queue_length);
            try{
                wait();
            }catch(InterruptedException ex){
                warn("Interrupted exception: "+ex.getMessage());
            }
        }
        setInternal_status(OCUPADO);
        debug("ya no esta mas ocupado! a ocuparlo :P pero en cola: "+queue_length);
        notify();//antes de devolver la db?
        return getDBConn();
    }
    public synchronized void devolverConector(){
       debug("SERVED!");
       if(getQueue_length() == 0){//me libere totalmente
           debug("totalmente desocupado notifying...");

       }else{
           decrementQueue();
           debug("en la cola quedan "+queue_length);
       }
       setInternal_status(DESOCUPADO);
       notify();//notifica al primer thread que este esperando
    }
    public synchronized void incrementQueue(){
       queue_length =queue_length+1;
    }
    public synchronized void decrementQueue(){
       queue_length =queue_length-1;
    }
    public synchronized int getQueue_length(){
       return queue_length;
    }
    public synchronized void setInternal_status(int internal_status){
       this.internal_status = internal_status;
    }
    public synchronized int getInternal_status(){
       return internal_status;
    }
    //</editor-fold>


    //<editor-fold defaultstate="collapsed" desc="debug">
    protected static final boolean DEBUG=false;

    protected static void debug(String text){
        if(DEBUG)
            System.out.println("DQM-DEBUG: "+Thread.currentThread().getName()+" -- "+text);
    }
    private static void warn(String message) {
        System.out.println("WARN - "+message);
    }
    //</editor-fold>

}
